﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class DUPhieuNhapBO : Csla.BusinessBase<DUPhieuNhapBO>
    {
        #region Business Properties

        private int? _DUPhieuNhapID = null;
        public int? DUPhieuNhapID
        {
            get { return this._DUPhieuNhapID; }
            set { this._DUPhieuNhapID = value; PropertyHasChanged("DUPhieuNhapID"); }
        }

        private string _MaSo = null;
        public string MaSo
        {
            get { return this._MaSo; }
            set { this._MaSo = value; PropertyHasChanged("MaSo"); }
        }

        private string _ChungTu = null;
        public string ChungTu
        {
            get { return this._ChungTu; }
            set { this._ChungTu = value; PropertyHasChanged("ChungTu"); }
        }

        private int? _NhaCungCapID = null;
        public int? NhaCungCapID
        {
            get { return this._NhaCungCapID; }
            set { this._NhaCungCapID = value; PropertyHasChanged("NhaCungCapID"); }
        }

        private int? _NguonNhapID = null;
        public int? NguonNhapID
        {
            get { return this._NguonNhapID; }
            set { this._NguonNhapID = value; PropertyHasChanged("NguonNhapID"); }
        }

        private DateTime? _NgayLapPhieu = null;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; PropertyHasChanged("NgayLapPhieu"); }
        }

        private string _NoiDung = null;
        public string NoiDung
        {
            get { return this._NoiDung; }
            set { this._NoiDung = value; PropertyHasChanged("NoiDung"); }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
        }

        private int? _NguoiLapPhieuID = null;
        public int? NguoiLapPhieuID
        {
            get { return this._NguoiLapPhieuID; }
            set { this._NguoiLapPhieuID = value; PropertyHasChanged("NguoiLapPhieuID"); }
        }

        private int? _KeToanID = null;
        public int? KeToanID
        {
            get { return this._KeToanID; }
            set { this._KeToanID = value; PropertyHasChanged("KeToanID"); }
        }

        private int? _NguoiRaLenhID = null;
        public int? NguoiRaLenhID
        {
            get { return this._NguoiRaLenhID; }
            set { this._NguoiRaLenhID = value; PropertyHasChanged("NguoiRaLenhID"); }
        }

        private int? _ThuKhoID = null;
        public int? ThuKhoID
        {
            get { return this._ThuKhoID; }
            set { this._ThuKhoID = value; PropertyHasChanged("ThuKhoID"); }
        }

        private int? _NguoiNhanHangID = null;
        public int? NguoiNhanHangID
        {
            get { return this._NguoiNhanHangID; }
            set { this._NguoiNhanHangID = value; PropertyHasChanged("NguoiNhanHangID"); }
        }

        private string _NguoiGiaoHang = null;
        public string NguoiGiaoHang
        {
            get { return this._NguoiGiaoHang; }
            set { this._NguoiGiaoHang = value; PropertyHasChanged("NguoiGiaoHang"); }
        }

        private double? _TongSo = null;
        public double? TongSo
        {
            get { return this._TongSo; }
            set { this._TongSo = value; PropertyHasChanged("TongSo"); }
        }

        private double? _ThanhTien = null;
        public double? ThanhTien
        {
            get { return this._ThanhTien; }
            set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
        }

        private double? _TienPhaiTra = null;
        public double? TienPhaiTra
        {
            get { return this._TienPhaiTra; }
            set { this._TienPhaiTra = value; PropertyHasChanged("TienPhaiTra"); }
        }

        private string _BangChu = null;
        public string BangChu
        {
            get { return this._BangChu; }
            set { this._BangChu = value; PropertyHasChanged("BangChu"); }
        }

        private double? _TienTra = null;
        public double? TienTra
        {
            get { return this._TienTra; }
            set { this._TienTra = value; PropertyHasChanged("TienTra"); }
        }

        private double? _TienThieu = null;
        public double? TienThieu
        {
            get { return this._TienThieu; }
            set { this._TienThieu = value; PropertyHasChanged("TienThieu"); }
        }

        private double? _TienPhatSinh = null;
        public double? TienPhatSinh
        {
            get { return this._TienPhatSinh; }
            set { this._TienPhatSinh = value; PropertyHasChanged("TienPhatSinh"); }
        }

        private string _LyDoPhatSinh = null;
        public string LyDoPhatSinh
        {
            get { return this._LyDoPhatSinh; }
            set { this._LyDoPhatSinh = value; PropertyHasChanged("LyDoPhatSinh"); }
        }

        private double? _MucVAT = null;
        public double? MucVAT
        {
            get { return this._MucVAT; }
            set { this._MucVAT = value; PropertyHasChanged("MucVAT"); }
        }

        private double? _MucGiamGia = null;
        public double? MucGiamGia
        {
            get { return this._MucGiamGia; }
            set { this._MucGiamGia = value; PropertyHasChanged("MucGiamGia"); }
        }

        private double? _MucPhucVu = null;
        public double? MucPhucVu
        {
            get { return this._MucPhucVu; }
            set { this._MucPhucVu = value; PropertyHasChanged("MucPhucVu"); }
        }

        private double? _MucChietKhau = null;
        public double? MucChietKhau
        {
            get { return this._MucChietKhau; }
            set { this._MucChietKhau = value; PropertyHasChanged("MucChietKhau"); }
        }

        private int? _ThanhToan = null;
        public int? ThanhToan
        {
            get { return this._ThanhToan; }
            set { this._ThanhToan = value; PropertyHasChanged("ThanhToan"); }
        }

        private int? _TinhTrangThanhToan = null;
        public int? TinhTrangThanhToan
        {
            get { return this._TinhTrangThanhToan; }
            set { this._TinhTrangThanhToan = value; PropertyHasChanged("TinhTrangThanhToan"); }
        }

        private int? _HinhThucThanhToanID = null;
        public int? HinhThucThanhToanID
        {
            get { return this._HinhThucThanhToanID; }
            set { this._HinhThucThanhToanID = value; PropertyHasChanged("HinhThucThanhToanID"); }
        }

        private int? _TinhTrangNhapKho = null;
        public int? TinhTrangNhapKho
        {
            get { return this._TinhTrangNhapKho; }
            set { this._TinhTrangNhapKho = value; PropertyHasChanged("TinhTrangNhapKho"); }
        }

        private int? _ThucHien = null;
        public int? ThucHien
        {
            get { return this._ThucHien; }
            set { this._ThucHien = value; PropertyHasChanged("ThucHien"); }
        }

        private int? _UserCreated = Session.LoginID;
        public int? UserCreated
        {
            get { return this._UserCreated; }
            set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
        }

        private DateTime? _DateCreated = DateTime.MaxValue;
        public DateTime? DateCreated
        {
            get { return this._DateCreated; }
            set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
        }

        private int? _UserModify = null;
        public int? UserModify
        {
            get { return this._UserModify; }
            set { this._UserModify = value; PropertyHasChanged("UserModify"); }
        }

        private DateTime? _DateModify = null;
        public DateTime? DateModify
        {
            get { return this._DateModify; }
            set { this._DateModify = value; PropertyHasChanged("DateModify"); }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; PropertyHasChanged("Deleted"); }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _DUPhieuNhapID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newDUPhieuNhapID = -1;
        private DUPhieuNhapBO()
        {
            _DUPhieuNhapID = newDUPhieuNhapID--;
        }

        public DUPhieuNhapBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public DUPhieuNhapBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static DUPhieuNhapBO NewRootDUPhieuNhapBO()
        {
            return DataPortal.Create<DUPhieuNhapBO>(new RootCriteria());
        }

        internal static DUPhieuNhapBO NewChildDUPhieuNhapBO()
        {
            return DataPortal.Create<DUPhieuNhapBO>(new ChildCriteria());
        }

        public static DUPhieuNhapBO GetRootDUPhieuNhapBO(int _DUPhieuNhapID)
        {
            return DataPortal.Fetch<DUPhieuNhapBO>(new RootCriteria(_DUPhieuNhapID));
        }

        public static DUPhieuNhapBO GetRootDUPhieuNhapBOByWhere(string where)
        {
            return DataPortal.Fetch<DUPhieuNhapBO>(where);
        }

        internal static DUPhieuNhapBO GetChildDUPhieuNhapBO(SqlDataReader dataReader)
        {
            return new DUPhieuNhapBO(dataReader);
        }

        public static void DeleteDUPhieuNhapBO(int _DUPhieuNhapID)
        {
            DataPortal.Delete(new RootCriteria(_DUPhieuNhapID));
        }

        public static void DeleteDUPhieuNhapBO(int _DUPhieuNhapID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_DUPhieuNhapID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int DUPhieuNhapID;
            public bool directDelete = false;
            public RootCriteria(int _DUPhieuNhapID)
            {
                this.DUPhieuNhapID = _DUPhieuNhapID;
                this.directDelete = false;
            }

            public RootCriteria(int _DUPhieuNhapID, bool directDelete)
            {
                this.DUPhieuNhapID = _DUPhieuNhapID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int DUPhieuNhapID;
            public bool directDelete = false;
            public ChildCriteria(int _DUPhieuNhapID)
            {
                this.DUPhieuNhapID = _DUPhieuNhapID;
                this.directDelete = false;
            }

            public ChildCriteria(int _DUPhieuNhapID, bool directDelete)
            {
                this.DUPhieuNhapID = _DUPhieuNhapID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuNhapBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[DUPhieuNhapID] = " + criteria.DUPhieuNhapID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuNhapBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["DUPhieuNhapID"] is DBNull))
                this._DUPhieuNhapID = (int)dataReader["DUPhieuNhapID"];
            if (!(dataReader["MaSo"] is DBNull))
                this._MaSo = (string)dataReader["MaSo"];
            if (!(dataReader["ChungTu"] is DBNull))
                this._ChungTu = (string)dataReader["ChungTu"];
            if (!(dataReader["NhaCungCapID"] is DBNull))
                this._NhaCungCapID = (int)dataReader["NhaCungCapID"];
            if (!(dataReader["NguonNhapID"] is DBNull))
                this._NguonNhapID = (int)dataReader["NguonNhapID"];
            if (!(dataReader["NgayLapPhieu"] is DBNull))
                this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];
            if (!(dataReader["NoiDung"] is DBNull))
                this._NoiDung = (string)dataReader["NoiDung"];
            if (!(dataReader["GhiChu"] is DBNull))
                this._GhiChu = (string)dataReader["GhiChu"];
            if (!(dataReader["NguoiLapPhieuID"] is DBNull))
                this._NguoiLapPhieuID = (int)dataReader["NguoiLapPhieuID"];
            if (!(dataReader["KeToanID"] is DBNull))
                this._KeToanID = (int)dataReader["KeToanID"];
            if (!(dataReader["NguoiRaLenhID"] is DBNull))
                this._NguoiRaLenhID = (int)dataReader["NguoiRaLenhID"];
            if (!(dataReader["ThuKhoID"] is DBNull))
                this._ThuKhoID = (int)dataReader["ThuKhoID"];
            if (!(dataReader["NguoiNhanHangID"] is DBNull))
                this._NguoiNhanHangID = (int)dataReader["NguoiNhanHangID"];
            if (!(dataReader["NguoiGiaoHang"] is DBNull))
                this._NguoiGiaoHang = (string)dataReader["NguoiGiaoHang"];
            if (!(dataReader["TongSo"] is DBNull))
                this._TongSo = (double)dataReader["TongSo"];
            if (!(dataReader["ThanhTien"] is DBNull))
                this._ThanhTien = (double)dataReader["ThanhTien"];
            if (!(dataReader["TienPhaiTra"] is DBNull))
                this._TienPhaiTra = (double)dataReader["TienPhaiTra"];
            if (!(dataReader["BangChu"] is DBNull))
                this._BangChu = (string)dataReader["BangChu"];
            if (!(dataReader["TienTra"] is DBNull))
                this._TienTra = (double)dataReader["TienTra"];
            if (!(dataReader["TienThieu"] is DBNull))
                this._TienThieu = (double)dataReader["TienThieu"];
            if (!(dataReader["TienPhatSinh"] is DBNull))
                this._TienPhatSinh = (double)dataReader["TienPhatSinh"];
            if (!(dataReader["LyDoPhatSinh"] is DBNull))
                this._LyDoPhatSinh = (string)dataReader["LyDoPhatSinh"];
            if (!(dataReader["MucVAT"] is DBNull))
                this._MucVAT = (double)dataReader["MucVAT"];
            if (!(dataReader["MucGiamGia"] is DBNull))
                this._MucGiamGia = (double)dataReader["MucGiamGia"];
            if (!(dataReader["MucPhucVu"] is DBNull))
                this._MucPhucVu = (double)dataReader["MucPhucVu"];
            if (!(dataReader["MucChietKhau"] is DBNull))
                this._MucChietKhau = (double)dataReader["MucChietKhau"];
            if (!(dataReader["ThanhToan"] is DBNull))
                this._ThanhToan = (int)dataReader["ThanhToan"];
            if (!(dataReader["TinhTrangThanhToan"] is DBNull))
                this._TinhTrangThanhToan = (int)dataReader["TinhTrangThanhToan"];
            if (!(dataReader["HinhThucThanhToanID"] is DBNull))
                this._HinhThucThanhToanID = (int)dataReader["HinhThucThanhToanID"];
            if (!(dataReader["TinhTrangNhapKho"] is DBNull))
                this._TinhTrangNhapKho = (int)dataReader["TinhTrangNhapKho"];
            if (!(dataReader["ThucHien"] is DBNull))
                this._ThucHien = (int)dataReader["ThucHien"];
            if (!(dataReader["UserCreated"] is DBNull))
                this._UserCreated = (int)dataReader["UserCreated"];
            if (!(dataReader["DateCreated"] is DBNull))
                this._DateCreated = (DateTime)dataReader["DateCreated"];
            if (!(dataReader["UserModify"] is DBNull))
                this._UserModify = (int)dataReader["UserModify"];
            if (!(dataReader["DateModify"] is DBNull))
                this._DateModify = (DateTime)dataReader["DateModify"];
            if (!(dataReader["Deleted"] is DBNull))
                this._Deleted = (bool)dataReader["Deleted"];

        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
            this._UserCreated = Session.LoginID;
            this._DateCreated = DateTime.MaxValue;
            this._UserModify = null;
            this._DateModify = null;
            this._Deleted = false;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuNhapBO_InsertBO");
            db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
            db.AddInParameter(cmd, "@ChungTu", SqlDbType.NVarChar, this._ChungTu);
            db.AddInParameter(cmd, "@NhaCungCapID", SqlDbType.Int, this._NhaCungCapID);
            db.AddInParameter(cmd, "@NguonNhapID", SqlDbType.Int, this._NguonNhapID);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@KeToanID", SqlDbType.Int, this._KeToanID);
            db.AddInParameter(cmd, "@NguoiRaLenhID", SqlDbType.Int, this._NguoiRaLenhID);
            db.AddInParameter(cmd, "@ThuKhoID", SqlDbType.Int, this._ThuKhoID);
            db.AddInParameter(cmd, "@NguoiNhanHangID", SqlDbType.Int, this._NguoiNhanHangID);
            db.AddInParameter(cmd, "@NguoiGiaoHang", SqlDbType.NVarChar, this._NguoiGiaoHang);
            db.AddInParameter(cmd, "@TongSo", SqlDbType.Float, this._TongSo);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@TienPhaiTra", SqlDbType.Float, this._TienPhaiTra);
            db.AddInParameter(cmd, "@BangChu", SqlDbType.NVarChar, this._BangChu);
            db.AddInParameter(cmd, "@TienTra", SqlDbType.Float, this._TienTra);
            db.AddInParameter(cmd, "@TienThieu", SqlDbType.Float, this._TienThieu);
            db.AddInParameter(cmd, "@TienPhatSinh", SqlDbType.Float, this._TienPhatSinh);
            db.AddInParameter(cmd, "@LyDoPhatSinh", SqlDbType.NVarChar, this._LyDoPhatSinh);
            db.AddInParameter(cmd, "@MucVAT", SqlDbType.Float, this._MucVAT);
            db.AddInParameter(cmd, "@MucGiamGia", SqlDbType.Float, this._MucGiamGia);
            db.AddInParameter(cmd, "@MucPhucVu", SqlDbType.Float, this._MucPhucVu);
            db.AddInParameter(cmd, "@MucChietKhau", SqlDbType.Float, this._MucChietKhau);
            db.AddInParameter(cmd, "@ThanhToan", SqlDbType.Int, this._ThanhToan);
            db.AddInParameter(cmd, "@TinhTrangThanhToan", SqlDbType.Int, this._TinhTrangThanhToan);
            db.AddInParameter(cmd, "@HinhThucThanhToanID", SqlDbType.Int, this._HinhThucThanhToanID);
            db.AddInParameter(cmd, "@TinhTrangNhapKho", SqlDbType.Int, this._TinhTrangNhapKho);
            db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
            db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
            db.AddOutParameter(cmd, "@NewDUPhieuNhapID", SqlDbType.Int, 10);
            db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);
            this._DUPhieuNhapID = (int)cmd.Parameters["@NewDUPhieuNhapID"].Value;
            this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
            this._UserModify = Session.LoginID;
            this._DateModify = DateTime.Now;

            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuNhapBO_UpdateBO");
            db.AddInParameter(cmd, "@DUPhieuNhapID", SqlDbType.Int, this._DUPhieuNhapID);
            db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
            db.AddInParameter(cmd, "@ChungTu", SqlDbType.NVarChar, this._ChungTu);
            db.AddInParameter(cmd, "@NhaCungCapID", SqlDbType.Int, this._NhaCungCapID);
            db.AddInParameter(cmd, "@NguonNhapID", SqlDbType.Int, this._NguonNhapID);
            db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
            db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
            db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
            db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
            db.AddInParameter(cmd, "@KeToanID", SqlDbType.Int, this._KeToanID);
            db.AddInParameter(cmd, "@NguoiRaLenhID", SqlDbType.Int, this._NguoiRaLenhID);
            db.AddInParameter(cmd, "@ThuKhoID", SqlDbType.Int, this._ThuKhoID);
            db.AddInParameter(cmd, "@NguoiNhanHangID", SqlDbType.Int, this._NguoiNhanHangID);
            db.AddInParameter(cmd, "@NguoiGiaoHang", SqlDbType.NVarChar, this._NguoiGiaoHang);
            db.AddInParameter(cmd, "@TongSo", SqlDbType.Float, this._TongSo);
            db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
            db.AddInParameter(cmd, "@TienPhaiTra", SqlDbType.Float, this._TienPhaiTra);
            db.AddInParameter(cmd, "@BangChu", SqlDbType.NVarChar, this._BangChu);
            db.AddInParameter(cmd, "@TienTra", SqlDbType.Float, this._TienTra);
            db.AddInParameter(cmd, "@TienThieu", SqlDbType.Float, this._TienThieu);
            db.AddInParameter(cmd, "@TienPhatSinh", SqlDbType.Float, this._TienPhatSinh);
            db.AddInParameter(cmd, "@LyDoPhatSinh", SqlDbType.NVarChar, this._LyDoPhatSinh);
            db.AddInParameter(cmd, "@MucVAT", SqlDbType.Float, this._MucVAT);
            db.AddInParameter(cmd, "@MucGiamGia", SqlDbType.Float, this._MucGiamGia);
            db.AddInParameter(cmd, "@MucPhucVu", SqlDbType.Float, this._MucPhucVu);
            db.AddInParameter(cmd, "@MucChietKhau", SqlDbType.Float, this._MucChietKhau);
            db.AddInParameter(cmd, "@ThanhToan", SqlDbType.Int, this._ThanhToan);
            db.AddInParameter(cmd, "@TinhTrangThanhToan", SqlDbType.Int, this._TinhTrangThanhToan);
            db.AddInParameter(cmd, "@HinhThucThanhToanID", SqlDbType.Int, this._HinhThucThanhToanID);
            db.AddInParameter(cmd, "@TinhTrangNhapKho", SqlDbType.Int, this._TinhTrangNhapKho);
            db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
            db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
            db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

            db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
            db.ExecuteNonQuery(cmd);

            this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_DUPhieuNhapID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DUPhieuNhapBO_DeleteBO");
            db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
            db.AddInParameter(cmd, "@DUPhieuNhapID", SqlDbType.Int, criteria.DUPhieuNhapID);
            db.ExecuteNonQuery(cmd);
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_DUPhieuNhapID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
